/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#ifndef __VAPI_H__
#define __VAPI_H__

// Includes
#include "../State/vState.h"
#include "../Viewport/vView.h"
#include "../Renderer/vRenderer.h"
#include "../Renderer/vPainter.h"
#include "../Geometry/vVertexBufferManager.h"
#include "./vTimer.h"
#include "../../Macros/vMemoryMacros.h"


namespace Vendetta {
	/**
	* Abstract API class.
	* Manages API dependent stuff.
	*/
	class vApi {
		friend class vEngine3D;
	public:

		/*------------- API dependent objects' classes -------------*/
		// Below only temporary objects' classes are listed
		#define API_OBJECT_TEXTURE_CREATOR 0 ///< cTextureCreator class
		#define API_OBJECT_FONT_CREATOR  1 ///< cTextureCreator class
		#define API_OBJECT_RENDER_TARGET 2 ///< cRenderTarget class

		/*------------------ Supported APIs ------------------*/
		#define API_GL  189438  ///< OpenGL (up to 1.4 version)
		#define API_D3D  666///< Direct 3D
		#define API_DEFAULT 1410  ///< Taken from environment at "API"
		#define API_NONE -666  ///< Requested when quitting

	public:
		/**
		* Updates API related stuff.
		* Listens for requests on screenshots etc.
		*/
		virtual vVoid Update();

		/**
		* Returns new API dependent object.
		* It does handle only short-life-time objects like cTextureCreator etc.
		* @param objectName "define" value for desired object
		* @return new object
		*/
		//virtual vVoid* newObject  (const vInt objectName) = 0;

		/**
		* Returns API Name (when Initialized).
		*
		* @return current rendering API's mName
		*/
		const vString& Name() const;

		/**
		* Takes screenshot and saves it to JPG files with consecutive ids.
		*/
		vVoid TakeScreenshot() const;

		/**
		* Swaps color buffers.
		*/
		virtual vVoid SwapColorBuffers() = 0;

	protected:
		/**
		* Default constructor.
		* Does not Initialize API!
		*/
		vApi();

		/**
		* Default destructor.
		* Does not Kill API!
		*/
		virtual ~vApi();

		/**
		* Initializes API!
		* @return true on success, false otherwise
		*/
		virtual vBool init() = 0;

		/**
		* Kills API
		* @return true on success, false otherwise
		*/
		virtual vVoid kill() = 0;

		/**
		* Handles errors coming from rendering device.
		*/
		virtual vVoid handleErrors() = 0;

		/**
		* Reads color buffer to given pointer (doesn't allocate memory,
		* but assumes it does have enough to shelter whole screen's
		* color buffer)
		* @param pixels pointer to array of unsigned chars to read pixels to
		*/
		virtual vVoid readColorBuffer(vUInt8* pixels) const = 0;

	protected:

		vString mName;///< API mName, e.g. "OpenGL", "D3D"

		vInt mBPP;///< Bits per pixel
		vInt mZBufferBPP;  ///< Z-buffer bits per pixel
		vInt mStencilBPP;  ///< Stencil buffer bits per pixel

	protected:
		/*!
		 * Shading state.
		 * Allows for changing shading state and keeps track of it.
		 */
		vState*  mpState;

		/*!
		 * Takes care of whole model-view and projection matrix stuff.
		 */
		vView*  mpView;

		/*!
		 * Provides a simple interface to draw simple things.
		 */
		vPainter* mpPainter;

		/*!
		 * Manages consecutive rendering stages.
		 * Used to render per-frame-buffered geometry from models.
		 */
		vRenderer* mpRenderer;

		/*!
		 * Manages vertex buffers.
		 */
		vVertexBufferManager* mpVertexBufferManager;

		/*!
		 * Manages time and frames mesurements.
		 */
		vTimer* mpTimer;
	};
}

#endif //__VAPI_H__